package cn.itlaobing.util;

import cn.itlaobing.model.PanationVir;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017-11-10.
 */
public class Paginate<T> extends SimpleTagSupport {
    private Logger log=Logger.getLogger(Paginate.class);
    private static final String REQUEST_URI_ATTRIBUTE_KEY="javax.servlet.forward.request_uri";
    private PanationVir<T> value;

    public void setValue(PanationVir<T> value) {
        this.value = value;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //可以简易理解为往jsp界面输出信息
        JspWriter writer=this.getJspContext().getOut();
        StringBuilder sb=new StringBuilder();
        sb.append("<ul class='pagination pagination-sm pull-center'>");
        //上一页
        generatePrevious(sb);
        //中间页
        generateNumber(sb);
        //下一页
        generateNext(sb);
        sb.append("</ul>");
        writer.write(sb.toString());

    }
    //针对上一页的操作，发现比第一页小了，就不能显示，比第一页大了就能显示，
    // 不能显示时，li标签的类属性就为disabled,有上一页就要显示相应的index
    public void generatePrevious(StringBuilder sb)
    {
        sb.append("<li class='"+(value.getFirst()?"":"disabled")+"'>");
        sb.append("<a href='"+(value.getFirst()?getHref(value.getFirstpage()):"#")+"' aria-label='上一页'>");
        sb.append("<span aria-hidden='true'>«</span>");
        sb.append("</a>");
        sb.append("</li>");
    }
    //下一页的内容和细节都一样
    public void generateNext(StringBuilder sb)
    {
        sb.append("<li class='"+(value.getLast()?"":"disabled")+"'>");
        sb.append("<a href='"+(value.getLast()?getHref(value.getLastpage()):"#")+"' aria-label='下一页'>");
        sb.append("<span aria-hidden='true'>»</span>");
        sb.append("</a>");
        sb.append("</li>");
    }
    //如果上一页有值，点击最前面的选项时，要获得当前页的所有提交信息，包括，路径，
    //最后综合成一个路径，然后生成
    public void generateNumber(StringBuilder sb)
    {
        for(int i=1;i<=value.getPage();i++)
        {
            sb.append("<li class='"+(value.getIndex()==i?"active":"")+"'>");
            sb.append("<a href='"+getHref(i)+"'>");
            sb.append(i);
            sb.append("</a>");
            sb.append("</li>");
        }
    }
    public String getHref(Integer index)
    {
        PageContext pageContext=(PageContext) this.getJspContext();
        HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
        String requestUri=request.getAttribute(REQUEST_URI_ATTRIBUTE_KEY).toString();
        Map<String,String[]> parameterMap=new HashMap(request.getParameterMap());
        parameterMap.remove("index");
        StringBuilder sb=new StringBuilder();
        for(Map.Entry<String,String[]> map:parameterMap.entrySet())
        {
            if(map.getValue().length>1)
            {
                for(int i=0;i<map.getValue().length;i++)
                {
                    sb.append(buildParam(map.getKey(),map.getValue()[i]));
                }
            }
            else
            {
                sb.append(buildParam(map.getKey(),map.getValue()[0]));
            }
        }
        if(sb.length()>1)
        {
            sb.deleteCharAt(sb.length()-1);
        }
        if(sb.length()>0)
        {
            return requestUri+"?index="+index+"&"+sb.toString();
        }
        return requestUri+"?index="+index;
    }
    //拼接字符串，
    public String buildParam(String paramKey,String paramValue)
    {
        return paramKey+"="+paramValue+"&";
    }
}











